package net.lab1024.smartadmin.module.business.vehicle.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.business.vehicle.dao.VehicleDao;
import net.lab1024.smartadmin.module.business.vehicle.domain.dto.VehicleAddDTO;
import net.lab1024.smartadmin.module.business.vehicle.domain.dto.VehicleQueryDTO;
import net.lab1024.smartadmin.module.business.vehicle.domain.dto.VehicleUpdateDTO;
import net.lab1024.smartadmin.module.business.vehicle.domain.entity.VehicleEntity;
import net.lab1024.smartadmin.module.business.vehicle.domain.vo.VehicleExcelVO;
import net.lab1024.smartadmin.module.business.vehicle.domain.vo.VehicleVO;
import net.lab1024.smartadmin.module.business.vehicleConfiguration.dao.VehicleConfigurationDao;
import net.lab1024.smartadmin.module.business.vehicleConfiguration.domain.entity.VehicleConfigurationEntity;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import net.lab1024.smartadmin.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ ���� ]
 *
 * @author �Ζ�
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:20:37
 * @since JDK1.8
 */
@Service
public class VehicleService {

    @Autowired
    private VehicleDao vehicleDao;
    @Autowired
    private VehicleConfigurationDao vehicleConfigurationDao;

    /**
     * ���ݱ�Ų�ѯ
     */
    public Boolean select(String sn,Long id){
        QueryWrapper<VehicleEntity> queryWrapper= new QueryWrapper<>();
        queryWrapper.lambda().like(VehicleEntity::getSn,sn);
        if(id!=null){
            queryWrapper.lambda().ne(VehicleEntity::getId, id);
        }
        return  vehicleDao.selectList(queryWrapper).size()>0;
    }

    /**
     * ����id��ѯ
     */
    public VehicleVO getById(Long id){
        return  vehicleDao.getById(id);
    }

    /**
     * ��ҳ��ѯ
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    public ResponseDTO<PageResultDTO<VehicleVO>> queryByPage(VehicleQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<VehicleVO> voList = vehicleDao.queryByPage(page, queryDTO);
        PageResultDTO<VehicleVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * ���
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    public ResponseDTO<String> add(VehicleAddDTO addDTO) {
        VehicleEntity entity = SmartBeanUtil.copy(addDTO, VehicleEntity.class);
        vehicleDao.insert(entity);
        VehicleConfigurationEntity VehicleConfigurationEntity = SmartBeanUtil.copy(addDTO, VehicleConfigurationEntity.class);
        VehicleConfigurationEntity.setVehicleId(entity.getId());
        vehicleConfigurationDao.insert(VehicleConfigurationEntity);
        return ResponseDTO.succ();
    }

    /**
     * �༭
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(VehicleUpdateDTO updateDTO) {
        VehicleEntity entity = SmartBeanUtil.copy(updateDTO, VehicleEntity.class);
        vehicleDao.updateById(entity);
        QueryWrapper<VehicleConfigurationEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(VehicleConfigurationEntity::getVehicleId,entity.getId());

        VehicleConfigurationEntity vehicleConfigurationEntity=vehicleConfigurationDao.selectOne(queryWrapper);
        vehicleConfigurationEntity.setBodyId(updateDTO.getBodyId());
        vehicleConfigurationEntity.setBatteryId(updateDTO.getBatteryId());
        vehicleConfigurationEntity.setDriveShaftId(updateDTO.getDriveShaftId());
        vehicleConfigurationEntity.setEngineId(updateDTO.getEngineId());
        vehicleConfigurationEntity.setGearboxId(updateDTO.getGearboxId());
        vehicleConfigurationEntity.setMotorId(updateDTO.getMotorId());
        vehicleConfigurationEntity.setTorqueConvertId(updateDTO.getTorqueConvertId());
        vehicleConfigurationEntity.setWheelId(updateDTO.getWheelId());
        vehicleConfigurationEntity.setTransaxleId(updateDTO.getTransaxleId());
            vehicleConfigurationDao.updateById(vehicleConfigurationEntity);
        return ResponseDTO.succ();
    }

    /**
     * ɾ��
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        vehicleDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * ��ѯȫ����������
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    public List<VehicleExcelVO> queryAllExportData(VehicleQueryDTO queryDTO) {
        return vehicleDao.queryAllExportData( queryDTO);
    }

    /**
     * ������ѯ��������
     * @author �Ζ�
     * @date 2021-11-19 11:20:37
     */
    public List<VehicleExcelVO> queryBatchExportData(List<Long> idList) {
        return vehicleDao.queryBatchExportData(idList);
    }
}
